﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;

using System.Reflection;
using System.IO;
//using System.Windows;

//using CAD.Service;
//using DotNetARX;

namespace CAD.Demo
{
    public class HelloCommand
    {

        /// <summary>
        /// hello basic
        /// </summary>
        [CommandMethod("CadHello")]
        public void CadHello()
        {
            //当前选项卡文档
            Document document = Application.DocumentManager.MdiActiveDocument;
            //编辑器
            Editor editor = document.Editor;
            editor.WriteMessage("HELLO FROM VS C#");//命令行文字

        }

        /// <summary>
        /// show view 
        /// </summary>
        [CommandMethod("ShowWpfView")]
        public void ShowWpfView()
        {
            int sum = 2;
            for (int i = 0; i < 100; i++)
            {
                sum += i;
            }
            DemoView demo = new DemoView();
            demo.Title = sum.ToString();
            demo.Show();
        }



        [CommandMethod("CadDbText")]
        public void CadDbText()
        {
            //当前选项卡文档
            Document document = Application.DocumentManager.MdiActiveDocument;
            Database database = HostApplicationServices.WorkingDatabase;

            //编辑器
            Editor editor = document.Editor;
            editor.WriteMessage($"\n ====================running CadDbText====================== \n ");//命令行文字

            //创建文字
            DBText text = new DBText();
            text.Rotation = 90;
            text.TextString = "first dbtext vs c#";
            text.Position = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);
            text.Height = 300;
            text.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(1, 122, 1);

            //SetFocus(document.Window.Handle);//提前调用
            //执行代码  

            HelloHelper.AddToModelSpace(database, text,true);

            //Action action = new Action(() => {  
            //    HelloHelper.AddToModelSpace(database, text);
            //});
            //action.Invoke();
            //using (DocumentLock docLock = document.LockDocument())
            //{
            //    //add to modelspace 加入模型空间
            //    action.Invoke();
            //}
            //对CAD系统命令
            //document.SendStringToExecute("Zoom E", true, false, false);

        }



        [CommandMethod("SelectEntity")]
        public IList<Entity> SelectEntity()
        {
            List<Entity> entitys = new List<Entity>();

            //-----------

            //当前选项卡文档
            Document document = Application.DocumentManager.MdiActiveDocument;
            //编辑器
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;


            PromptSelectionResult promptSelRes = editor.SelectImplied();//得到选择集状态
            SelectionSet selectionSet;

            //判断是否有已选项  其实反正都要清空选择  此步判断可跳过
            //if (promptSelRes.Status == PromptStatus.OK)
            //{
            //    selectionSet = promptSelRes.Value;
            //    editor.WriteMessage($"selected num :{selectionSet.Count.ToString()}");
            //}
            //else
            //{
            //    editor.WriteMessage($"selected num :0");
            //}

            //创建空数组
            ObjectId[] emptyObjectId = new ObjectId[0];
            editor.SetImpliedSelection(emptyObjectId);  //设置选择集 指向空数组

            //开始请求从图形工程界面选择图元
            promptSelRes = editor.GetSelection();

            //判断是否选中
            if (promptSelRes.Status == PromptStatus.OK)
            {
                selectionSet = promptSelRes.Value;
                editor.WriteMessage($"selected num :{selectionSet.Count.ToString()}");
                List<ObjectId> objectIds = selectionSet.GetObjectIds().ToList();

                Database database = HostApplicationServices.WorkingDatabase;
                using (Transaction transaction = database.TransactionManager.StartTransaction())
                {
                    objectIds.ForEach(oid => entitys.Add((Entity)oid.GetObject(OpenMode.ForRead)));
                    transaction.Commit();
                }
            }
            else
            {
                editor.WriteMessage($"selected num :0");
            }

            return entitys;
        }


        [CommandMethod("EditEntity")]
        public void EditEntity()
        {
            Entity entity = SelectEntity()[0];
            if (entity != null && entity is Circle)
            {
                Circle circle = entity as Circle;
                if (circle != null)
                {
                    Database database = HostApplicationServices.WorkingDatabase;
                    using (Transaction transaction = database.TransactionManager.StartTransaction())
                    {
                        circle = (Circle)entity.ObjectId.GetObject(OpenMode.ForWrite);
                        circle.Radius = 10;
                        transaction.Commit();
                    }
                }
            }
        }





        /// <summary>
        /// dock可停靠窗口
        /// </summary>
        [Autodesk.AutoCAD.Runtime.CommandMethod("AddMyPaletteSet")]
        public void AddMyPaletteSet()
        {
            Autodesk.AutoCAD.Windows.PaletteSet myPaletteSet
                = new Autodesk.AutoCAD.Windows.PaletteSet("我的PallteSet");

            myPaletteSet.Add("我的工具集", new System.Windows.Forms.Control("我的工具集"));
            myPaletteSet.Add("我的颜色", new System.Windows.Forms.Control("我的颜色"));
            myPaletteSet.Add("我的控件", new System.Windows.Forms.Control("我的控件"));

            myPaletteSet.Visible = true;
        }

    }

}
